package Chat;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JList;
import javax.swing.JTextArea;

/**
 *
 * @author danielsl
 * "UnicastRemoteObject is used for exporting a remote object with JRMP and obtaining a stub that communicates to the remote object".
 * From http://download.oracle.com/javase/1.5.0/docs/api/java/rmi/server/UnicastRemoteObject.html
 */
public class CallbackImpl extends UnicastRemoteObject implements Callback {

    String userName;
    JTextArea area;
    int quizScore = 0;
    Boolean updateList = false;
    
    public CallbackImpl() throws RemoteException {
    }

    public String timestamp() {
        Date todaysDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("[HH:mm:ss] ");
        return formatter.format(todaysDate);
    }

    public void connectMessage(String userName) throws RemoteException {
        System.out.println(timestamp() + userName + " has connected.");
        if (area != null) {
            area.append(timestamp() + userName + " has connected.\n");
            updateList = true;
        }
    }

    public void broadcastMessage(String message, String userName) throws RemoteException {
        System.out.println(timestamp() + userName + " wrote: " + message);
        if (area != null) {
            area.append(timestamp() + userName + " wrote: " + message + "\n");
        }

    }

      public void whisperMessage(String message, String toName, String fromName) throws RemoteException {
        System.out.println(timestamp() + fromName + " whispers: " + message);
        if (area != null) {
            area.append(timestamp() + fromName + " whispers: " + message + "\n");
        }
    }

    public void botMessage(String message, String botName) throws RemoteException {
        System.out.println(timestamp() + "The bot " + botName + " whispers you: " + message);
        if (area != null) {
            area.append(timestamp() + "The bot " + botName + " whispers you: " + message + "\n");
        }

    }

    public void rename(String newName, String userName) throws RemoteException {
        System.out.println(timestamp() + userName + " changed name to: " + newName);
        if (area != null) {
            area.append(timestamp() + userName + " changed name to: " + newName + "\n");
            updateList = true;
        }
    }

    public void quitMessage(String userName) throws RemoteException {
        System.out.println(timestamp() + userName + " has left the chat.");
        if (area != null) {
            area.append(timestamp() + userName + " has left the chat.\n");
            updateList = true;
        }
    }

    public boolean getUpdateList() throws RemoteException {
        return updateList;
    }

    public String getUserName() throws RemoteException {
        return userName;
    }

    public void setUserName(String userName) throws RemoteException {
        this.userName = userName;
    }

    public void setTextArea(javax.swing.JTextArea area) throws RemoteException {
        this.area = area;
    }
    public void setUpdateList(boolean updateList) throws RemoteException {
        this.updateList =  updateList;
    }

    public int getQuizScore() throws RemoteException {
        return quizScore;
    }

    public void setQuizScore() throws RemoteException {
        this.quizScore++;
    }

    public void deleteQuizScore() throws RemoteException {
        quizScore = 0;
    }


}
